package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.han.bom.Exam;
import com.han.bom.MultipleChoice;
import com.han.bom.Professor;
import com.han.bom.Question;
import com.han.bom.Student;
import com.han.dao.concrete.product.AnswerDAO;
import com.han.dao.concrete.product.ExamDAO;
import com.han.dao.concrete.product.QuestionDAO;
import com.han.dao.concrete.product.UserDAO;

/**
 * Servlet implementation class SearchServlet
 */
@WebServlet("/Professor/ProfessorSearchServlet")
public class ProfessorSearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfessorSearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String searchQuery = null;
		
		if(request.getParameter("searchKey")!=null){
			searchQuery = request.getParameter("searchKey");
		}
		
		HttpSession session = request.getSession();
		
		Professor professor = (Professor)session.getAttribute("professor");
		
		UserDAO userdao = new UserDAO();
		ExamDAO examdao = new ExamDAO();
		QuestionDAO questiondao = new QuestionDAO();
		AnswerDAO answerdao = new AnswerDAO();
		
		try {
			ArrayList<Student> students = userdao.retrieveStudentsByKeyword(searchQuery, professor.getUserID());
			ArrayList<Question> questions = questiondao.retrieveQuestionsbyKeyword(searchQuery);
			ArrayList<Exam> exams = examdao.retrieveExamsByKeyword(searchQuery, professor.getUserID());
			
			for(Question question: questions){
				ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
				
				question.setAnswer(choices.get(0));
				
				if(question instanceof MultipleChoice){
					Collections.sort(choices);
				}
				else{
					Collections.sort(choices);
					Collections.reverse(choices);
				}
			}
			
			for(Exam exam: exams){
				
				exam.setQuestions(questiondao.retrieveQuestionsbyExam(exam.getExamID()));
				
				for(Question question: exam.getQuestions()){
					ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
					
					question.setAnswer(choices.get(0));
					
					if(question instanceof MultipleChoice){
						Collections.sort(choices);
					}
					else{
						Collections.sort(choices);
						Collections.reverse(choices);
					}
				}
				
			}
			
			request.setAttribute("students", students);
			request.setAttribute("exams", exams);
			request.setAttribute("questions", questions);
			
//			System.out.println(students);
//			System.out.println(exams);
//			System.out.println(questions);
			
			RequestDispatcher rd = request.getRequestDispatcher("Search.jsp");
			rd.forward(request, response);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
